<?php
/**
 * 抽奖
 */
namespace app\api\model;
use app\common\model\DrawLog as DrawLogModel;

class DrawLog extends DrawLogModel
{
    /**
     * 获取抽奖次数
     * @param $mid 用户ID
     */
    public static function frequency($mid){
//        self::where('mid',$mid)->where('draw_name','')
        $arr = array(
            array('id'=>1,'name'=>'特等奖','v'=>1),
            array('id'=>2,'name'=>'一等奖','v'=>5),
            array('id'=>3,'name'=>'二等奖','v'=>10),
            array('id'=>4,'name'=>'三等奖','v'=>12),
            array('id'=>5,'name'=>'四等奖','v'=>22),
            array('id'=>6,'name'=>'没中奖','v'=>50)
        );
        $id = self::get_rand($arr);

    }

    //执行抽奖
    public static function luck($mid){

    }

    function get_rand($proArr) {
        $result = array();
        foreach ($proArr as $key => $val) {
            $arr[$key] = $val['v'];
        }

        // 概率数组的总概率
        $proSum = array_sum($arr);
        asort($arr);

        // 概率数组循环
        foreach ($arr as $k => $v) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $v) {
                $result = $proArr[$k];
                break;
            } else {
                $proSum -= $v;
            }
        }
        return $result;
    }
}